package com.cxyd.modules.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.constant.Constant;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.BaseServiceImpl;
import com.cxyd.modules.log.dao.SysLogApiDao;
import com.cxyd.modules.log.dto.SysLogApiDTO;
import com.cxyd.modules.log.entity.SysLogApiEntity;
import com.cxyd.modules.log.service.SysLogApiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysLogApiServiceImpl extends BaseServiceImpl<SysLogApiDao, SysLogApiEntity> implements SysLogApiService {
    @Override
    public PageData<SysLogApiDTO> page(Map<String, Object> params) {
        IPage<SysLogApiEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, SysLogApiDTO.class);
    }

    @Override
    public List<SysLogApiDTO> list(Map<String, Object> params) {
        return null;
    }

    private QueryWrapper<SysLogApiEntity> getWrapper(Map<String, Object> params) {
        String apiName = (String) params.get("apiName");
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");

        QueryWrapper<SysLogApiEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(apiName), "api_name", apiName);
        wrapper.ge(StringUtils.isNotBlank(startDate), "create_date", startDate);
        wrapper.le(StringUtils.isNotBlank(endDate), "create_date", endDate);

        return wrapper;
    }

    @Override
    public void save(SysLogApiEntity entity) {
        insert(entity);
    }
}
